﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RememberLib
{
    public class BookBusiness
    {
        static BookBusiness _BookBusiness = null;
        public static BookBusiness Instance()
        {
            if (_BookBusiness == null)
            {
                _BookBusiness = new BookBusiness();
            }

            return _BookBusiness;

        }

        RememberEntities context = new RememberEntities();
        public List<CategoryList> GetBookList(int parentID)
        {
            var list = context.CategoryList.Where(c => c.CategoryLevel == (int)CategoryLevel.Book  );

            if (parentID > 0)
            {
                list = list.Where(c => c.ParentID == parentID);
            }
            return list.ToList ();
        }

        /// <summary>
        /// State the item count and save book information for categroy
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public int StatBookItemTotal(int bookID)
        {
            var cat = GetCategory(bookID);
            int count = context.RememberItem.Where(c => c.ItemStatus == 1 && c.Book == cat.CategoryID).Count();

            if (count > 0)
            {
                cat.ItemTotal = count;
                SaveCategory(cat);
            }

            return count;
        }


        public List<CategoryList> GetLessonList(int parentID)
        {
            var list = context.CategoryList.Where(c => c.CategoryLevel == (int)CategoryLevel.Lesson );

            if (parentID > 0)
            {
                list = list.Where(c => c.ParentID == parentID);
            }

            return list.ToList ();
        }

        public List<CategoryList> GetLessonByNumber( string number)
        {
            var list = context.CategoryList.Where(c => c.CategoryLevel == (int)CategoryLevel.Lesson);

            if (!string.IsNullOrEmpty ( number ))
            {
                list = list.Where(c => c.CategoryNumber == number);
            }

            return list.ToList ();
        }

        public CategoryList GetLesson( int categoryID)
        {
            var list = context.CategoryList.Where(c => c.CategoryID == categoryID);
            if (list.Count() > 0)
            {
                return list.First();
            }

            return null;
        }

        public CategoryList GetCategory(int categoryID)
        {
            var list = context.CategoryList.Where(c => c.CategoryID == categoryID);
            if (list.Count() > 0)
            {
                return list.First();
            }

            return null;
        }

        public void SaveCategory(CategoryList category)
        {
            if (category == null)
                return;

            //int itemID = _Item
            string loginID = ""; //&& c.LoginID == loginID
            var list = context.CategoryList.Where(c => c.CategoryID == category.CategoryID);

            CategoryList cate = null;
            if (list != null && list.Count() > 0)
            {
                cate = list.First();
            }
            if (cate == null)
            {
                context.AddToCategoryList(category);
            }
            else
            {
                cate.CategoryID = category.CategoryID;
                cate.CategoryDesc = category.CategoryDesc;

                cate.CategoryName = category.CategoryName;
                cate.CategoryIndex = category.CategoryIndex;
                cate.CategoryLevel = category.CategoryLevel;
                cate.CategorySound = category.CategorySound;
                cate.ParentID = category.ParentID;
            }
            int rc = context.SaveChanges();

        }



    }
}
